/**
 * 
 */
package com.husky.powerbugz;

import java.util.Date;

/**
 * 记录bug信息
 *
 * @author cathy0048
 * @date 2012-9-25
 */
public class Bug {
	
	/**
	 * bug的id
	 */
	private int id = -1;
	
	/**
	 * bug标题 
	 */
	private String title = "";
	
	/**
	 * bug描述
	 */
	private String desc = "";
	
	/**
	 * bug注释
	 */
	private String comment = "";
	
	/**
	 * bug状态
	 */
	private BugState state = BugState.NEW;
	
	/**
	 * bug等级
	 */
	private BugLevel level = BugLevel.GENERAL;
	
	/**
	 * bug提交者
	 */
	private User author = null;
	
	/**
	 * bug提交时间
	 */
	private Date createdTime = new Date();
	
	/**
	 * bug修改时间
	 */
	private Date modifiedTime = new Date();
	
	/**
	 * bug所属项目
	 */
	private Project project = null;

	/**
	 * 获得bug的id,默认值为-1
	 * 
	 * @return id
	 */
	public int getId() {
		return id;
	}

	/**
	 * 设置bug的id
	 * 
	 * @param id
	 * @throws IllegalArgumentException 当id小于0时，抛出此异常
	 */
	public void setId(int id) {
		if(id < 0) {
			throw new IllegalArgumentException("Id could not be less than 0. Invalid id: " + id);
		}
		this.id = id;
	}

	/**
	 * 获得bug标题
	 * 
	 * @return title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * 设置bug的title
	 * 
	 * @param title
	 * @throws IllegalArgumentException 当title为null或空时，抛出异常
	 */
	public void setTitle(String title) {
		if(title == null || title.isEmpty()) {
			throw new IllegalArgumentException("Title could not be empty. Invalid title: " + title);
		}
		this.title = title;
	}

	/**
	 * 获得bug描述
	 * 
	 * @return desc
	 */
	public String getDesc() {
		return desc;
	}

	/**
	 * 设置bug描述
	 * 
	 * @param desc
	 */
	public void setDesc(String desc) {
		this.desc = (desc != null) ? desc : "";
	}

	/**
	 * 获取bug注释
	 * 
	 * @return comment
	 */
	public String getComment() {
		return comment;
	}

	/**
	 * 设置bug注释
	 * 
	 * @param comment
	 */
	public void setComment(String comment) {
		this.comment = (comment != null) ? comment : "";
	}

	/**
	 * 获得bug状态,默认返回新建状态：{@link BugState#NEW}
	 * 
	 * @return state
	 */
	public BugState getState() {
		return state;
	}

	/**
	 * 设置bug状态
	 * 
	 * @param state
	 * @throws NullPointerException 当state为null时，抛出此异常
	 */
	public void setState(BugState state) {
		if(state == null) {
			throw new NullPointerException("Bug state could not be null.");
		}
		this.state = state;
	}

	/**
	 * 获得bug等级，默认返回普通级别：{@link BugLevel#GENERAL}
	 * 
	 * @return level
	 */
	public BugLevel getLevel() {
		return level;
	}

	/**
	 * 设置bug等级
	 * 
	 * @param level
	 * @throws NullPointerException 当level为null时，抛出此异常
	 */
	public void setLevel(BugLevel level) {
		if(level == null) {
			throw new NullPointerException("Bug level could not be null.");
		}
		this.level = level;
	}

	/**
	 * 获得bug提交者，如果创建人不存在，则返回null
	 * 
	 * @return author
	 */
	public User getAuthor() {
		return author;
	}

	/**
	 * 设置bug提交者
	 * 
	 * @param author
	 */
	public void setAuthor(User author) {
		this.author = author;
	}

	/**
	 * 获得bug提交时间，该方法不会放回null
	 * 
	 * @return createdTime
	 */
	public Date getCreatedTime() {
		return createdTime;
	}

	/**
	 * 设置bug提交时间
	 * 
	 * @param createdTime
	 * @throws NullPointerException 当createdTime为null，抛出此异常
	 */
	public void setCreatedTime(Date createdTime) {
		if(createdTime == null) {
			throw new NullPointerException("Bug created time could not be null.");
		}
		this.createdTime = createdTime;
	}

	/**
	 * 获取bug修改时间，该方法不会返回null
	 * 
	 * @return modifiedTime
	 */
	public Date getModifiedTime() {
		return modifiedTime;
	}

	/**
	 * 设置bug修改时间
	 * 
	 * @param modifiedTime
	 * @throws NullPointerException 当modifiedTime为null，抛出此异常
	 */
	public void setModifiedTime(Date modifiedTime) {
		if(modifiedTime != null) {
			throw new NullPointerException("Bug modified time could not be null.");
		}
		this.modifiedTime = modifiedTime;
	}

	/**
	 * 获得bug所属项目，如果该bug未设置项目，则返回null
	 * 
	 * @return project
	 */
	public Project getProject() {
		return project;
	}

	/**
	 * 设置bug所属项目
	 * 
	 * @param project
	 */
	public void setProject(Project project) {
		if(project == null) {
			
		}
		this.project = project;
	}

	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder(); 
		builder.append("Bug [id=").append(id);
		builder.append(", title=").append(title);
		builder.append(", desc=").append(desc);
		builder.append(", comment=").append(comment);
		builder.append(", state=").append(state);
		builder.append(", level=").append(level);
		builder.append(", author=").append(author);
		builder.append(", createdTime=").append(createdTime);
		builder.append(", modifiedTime=").append(modifiedTime);
		builder.append(", project=").append(project);
		return builder.toString();	
	}
	
}